package com.jshongli.myapplication.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.jshongli.myapplication.R;

import java.util.List;

public class ArrayPopWindow extends PopupWindow {

    private View mMenuView;
    private ListView listView;
    private TopArrayPopAdapter adapter;
    private List<String> strs;
    private ITopArrayPopWindowListener topArrayPopWindowListener;
    private int selectedIndex = 0;

    public ArrayPopWindow(Activity context, List<String> array) {
        super(context);

        this.strs = array;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.top_array_popwindow, null);

        LinearLayout lay = (LinearLayout)mMenuView.findViewById(R.id.list_layout);
        lay.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                ArrayPopWindow.this.dismiss();

            }

        });

        listView = (ListView) mMenuView.findViewById(R.id.pop_listview);
        adapter = new TopArrayPopAdapter(context, array);
        selectedIndex = 0;
        listView.setAdapter(adapter);

        if(array.size()<=10){
            setListViewHeightBasedOnChildren(listView);
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                selectedIndex = arg2;
                if(topArrayPopWindowListener!=null){
                    topArrayPopWindowListener.onDataChanage(strs.get(selectedIndex),selectedIndex);
                }
                adapter.current = selectedIndex;
                adapter.notifyDataSetChanged();
            }
        });
        this.setContentView(mMenuView);

        // popwindow设置
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00FFFFFF);
        this.setBackgroundDrawable(dw);
    }



    public int getSelectedIndex(){
        return selectedIndex;
    }

    public void setTopArrayPopWindowListener(ITopArrayPopWindowListener l) {
        this.topArrayPopWindowListener = l;
    }

    public void setCurrentSelectedIndex(int index){
        if(index<strs.size()){
            selectedIndex = index;
            adapter.current = selectedIndex;
            adapter.notifyDataSetChanged();
        }
    }

    class TopArrayPopAdapter extends BaseAdapter {
        Context context;
        List<String> listData;
        int current;

        public TopArrayPopAdapter(Context context, List<String> strs) {
            this.context = context;
            this.listData = strs;
            current=0;
        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            final int pos = position;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.top_array_popwindow_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.poplist_item_txt = (TextView) convertView.findViewById(R.id.poplist_item_txt);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.poplist_item_txt.setText(listData.get(pos));
            if (pos == current) {
                viewHolder.poplist_item_txt.setTextColor(ContextCompat.getColor(context,R.color.white));
                viewHolder.poplist_item_txt.setBackground(ContextCompat.getDrawable(context,R.drawable.blue18radius));
            } else {
                viewHolder.poplist_item_txt.setTextColor(ContextCompat.getColor(context,R.color.txt_normal_black));
                viewHolder.poplist_item_txt.setBackground(ContextCompat.getDrawable(context,R.drawable.white8radius));
            }

            return convertView;
        }

        final class ViewHolder {
            TextView poplist_item_txt;
        }
    }

    @Override
    public void showAsDropDown(View anchor){
        if(Build.VERSION.SDK_INT >= 24){
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public interface ITopArrayPopWindowListener {
        public void onDataChanage(String data,int i);
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽�?
            totalHeight += listItem.getMeasuredHeight(); // 统计�?��子项的�?高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高�?
        // params.height�?��得到整个ListView完整显示�?��的高�?
        listView.setLayoutParams(params);
    }

}